package root.electronclass.electronclass.grade.model

import android.os.Parcelable
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
import root.electronclass.electronclass.mvp.GlobalParameter

/**
 * 班级实体类集合
 * @author Lokiy
 * @date 2023/2/4 13:58
 */

/**
 * 通知查询条件
 */
data class NoticeQuery(
    val departId: Long = GlobalParameter.getEcardInfo().classId,
//    val level: Int = 0,//重要程度 1-普通 2-重要 3-全屏通知
    val pageNo: Int,
    val pageSize: Int = 20,
    val schoolId: Long = GlobalParameter.getEcardInfo().schoolId,
    val title: String? = null,//标题搜索
    val type: Int,//0-全部 1-近7天 2-近30天 3-一学期
)

data class PageResult<T>(val records: List<T>, val pages: Int, val current: Int)

data class ClassNotification(
    val id: Long,
    val picList: Array<MediaVo>?,
    val title: String,
    @SerializedName("readFlag")
    var status: Int,//1-未读 2-已读
    val level: Int,//重要程度 1-普通 2-重要 3-全屏通知
    @SerializedName("creator")
    val author: String,
    val creatorId: Long,
    val createTime: String,
    val content: String,
    val relationId: String,//已读未读表id
) {
    val levelText: String
        get() = when (level) {
            2 -> "重要"
            3 -> "全屏通知"
            else -> "普通"
        }
}

@Parcelize
data class MediaVo(val url: String) : Parcelable

/**
 * 通知查询条件
 */
data class HonorQuery(
    val departId: Long = GlobalParameter.getEcardInfo().classId,
    val pageNo: Int,
    val pageSize: Int = 20,
    val schoolId: Long = GlobalParameter.getEcardInfo().schoolId,
    val search: String? = null,//搜索
    val title: String? = null,//标题搜索
    val type: Int,//0-全部 1-近7天 2-近30天 3-一学期
    val userType: Int = 0,//1-学生 2-老师
)

/**
 * 班级荣誉Data
 */
@Parcelize
data class ClassHonor(
    val id: Int,
    val picList: Array<MediaVo>?,
    val title: String,//荣誉名称
    val type: Int,//荣誉类型 1-个人 2-集体
    @SerializedName("name")
    val belong: String,//获奖人姓名
    @SerializedName("date")
    val time: String,//获奖日期
    @SerializedName("company")
    val org: String,//发奖单位
    val createTime: String,
    val reason: String?,//荣誉缘由
    val userList: List<Person>?,//获奖人列表
    val userType: Int,//1-学生 2-老师
) : Parcelable

/**
 * 通知查询条件
 */
data class RecordQuery(
    val departId: Long = GlobalParameter.getEcardInfo().classId,
    val pageNo: Int,
    val pageSize: Int = 20,
    val schoolId: Long = GlobalParameter.getEcardInfo().schoolId,
    val search: String? = null,//搜索
    val title: String? = null,//标题搜索
    val type: Int,//0-全部 1-近7天 2-近30天 3-一学期
    val userType: Int = 0,//1-学生 2-老师
)

/**
 * 班级风采Data
 */
@Parcelize
data class ClassRecord(
    val picList: Array<MediaVo>?,//  多个由,分开
    val title: String,
    @SerializedName("happenedDate")
    val time: String,
    @SerializedName("content")
    val desc: String
) : Parcelable

/**
 * 课程表Data
 */
@Parcelize
data class ClassCurriculumItem(
    @SerializedName("subject")
    val subject: String,
    val startTime: String,
    val endTime: String,
    @SerializedName("name")
    val teacherName: String? = "",
    val photo: String? = "",
    val teacher: Person? = null,
    val gender: Int = 0,//性别，0-未填写；1-男；2-女
    val status: Int = 0,//0未签到 1已签到
    var position: Int = 0,//第几节课
    val id: Long = 0L,
) : Parcelable {
    val showTime: String
        get() {
            return if (startTime.isNotEmpty() && endTime.isNotEmpty()) {
                "$startTime-$endTime"
            } else {
                ""
            }
        }

    companion object {
        val EMPTY = ClassCurriculumItem("", "", "", "", "", null)
    }
}

/**
 * 值日项
 */
data class ClassRota(
    val id: String?,
    val item: String,
    val color: String? = null,//颜色
    val subSchoolId: String = GlobalParameter.getEcardInfo().schoolId.toString(),
)

data class RotaUser(
    val itemIds: List<Int>,//值日项id
    val userIds: List<Long>,//人员id
    val weekList: List<Int>,//1-7代表周一到周日
)

/**
 * 值日表数据
 */
data class ClassRotaItem(
    val dutyItem: String, //值日项
    val facePhoto: String?,//人脸照片
    val gender: Int,//性别，0-未填写；1-男；2-女
    val name: String,
    val userNo: String,
    val color: String?,
    @Deprecated("")
    var hasSign: Boolean = false/*是否签到*/
)

/**
 * 通知查询条件
 */
data class HomeworkStatQuery(
    val pageNo: Int,
    val startTime: String,
    val endTime: String,
    val status: Int,//1-待开始 2-进行中 3-部分未完成 4-全部已完成 5-已考核
    val departId: Long = GlobalParameter.getEcardInfo().classId,
    val pageSize: Int = 2000,
)


/**
 * 通知查询条件
 */
data class HomeworkDetailQuery(
    val pageNo: Int,
    val homeworkId: String,
    val search: String,
    val status: Int = 0,//1-待开始 2-进行中 3-部分未完成 4-全部已完成 5-已考核
    val departId: Long = GlobalParameter.getEcardInfo().classId,
    val pageSize: Int = 2000,
)

/**
 * 家庭作业
 */

@Parcelize
data class ClassHomeworkItem(
    val id: String,
    @SerializedName("title")
    val name: String?,
    @SerializedName("subjectName")
    val subject: String?,
    val startTime: String?,
    val endTime: String?,//截止时间
    @SerializedName("remark")
    val note: String?,
    val content: String?,//作业内容
    val picList: Array<MediaVo>?,//图片地址
    val videoList: Array<MediaVo>?,
    val creator: String?,//创建人姓名
    @SerializedName("status")//1-待开始 2-进行中 3-部分未完成 4-全部已完成 5-已考核
    val status: Int,//1-待开始 2-进行中 3-部分未完成 4-全部已完成 5-已考核
    @SerializedName("hour")
    val elapsedTime: Float,//预估小时
    @SerializedName("realHour")
    val avgTime: Float,//平均时间
    val createTime: String? = "",
) : Parcelable {

    val statusText: String
        get() = convertStatusToText(status)

    companion object {
        fun convertStatusToText(status: Int) = when (status) {
            //1-待开始 2-进行中 3-部分未完成 4-全部已完成 5-已考核
            1 -> "待开始"
            2 -> "进行中"
            3 -> "部分未完成"
            4 -> "全部已完成"
            5 -> "已考核"
            else -> "全部"
        }
    }
}


/**
 * 通知查询条件
 */
data class ExcellentPageQuery(
    val pageNo: Int,
    val startDate: String,
    val search: String,
    val endDate: String,
    val departId: Long = GlobalParameter.getEcardInfo().classId,
    val pageSize: Int = 2000,
)

@Parcelize
data class ClassHomeworkStudentItem(val student: Person, val status: Int, val completeTime: Long, val score: Int) : Parcelable


data class ClassHomeworkUser(
    val id: Long = 0,
    val finishTime: String? = null,
    var hour: Float? = null,//实际完成时间
    val preHour: Float? = null,//预估耗时
    val score: String? = null,
    val status: Int? = null,//1-未完成 2-已完成
    val userNo: String? = null,
    val name: String? = null,
    var comment: String? = null,//评价
    val homeworkId: String? = null,
    val photo: String? = null,
    val userId: String? = null,
    var subjectName: String = "",
    var finishFlag: Int = 1,//是否晚自习第二节下课完成， 1-是 2-否
)

data class ClassHomeworkDepart(
    var comment: String? = null,//评价
    val departId: Long = 0,
    val departName: String? = "",
    var finishFlag: Int = 1,//是否晚自习第二节下课完成， 1-是 2-否
    val grade: String? = "",
    val homeworkId: String? = null,
    var hour: Float? = null,//实际完成时间
    val id: Long = 0,
    val schoolId: Long = 0,
    val subjectId: Long = 0,
    var subjectName: String? = "",
)

data class ClassSubmitHomework(
    var type: Int,//1-个人 2-班级
    val list: List<ClassHomeworkUser>? = null,
    val person: ClassHomeworkUser? = null,
    val subjectId: Long,
)

data class ClassSubmitHomework2(
    var type: Int,//1-个人 2-班级
    val list: List<ClassHomeworkUser>? = null,//班级单科
    val list2: List<ClassHomeworkDepart>? = null,//班级多科
    val person: ClassHomeworkUser? = null,//个人单科
    val personList: List<ClassHomeworkDepart>? = null,//个人多科
    var subjectId: Long,//作业id
    var subjectType: Int,//1-单科 2-多科
)

data class ClassSubmitHomeworkSubject(
    var id: Long,//1-个人 2-班级
    val subjectName: String? = null,
)

/**
 * 人员
 */
@Parcelize
data class Person(
    val name: String,//名字
    val id: String = "",
    @SerializedName("facePhoto", alternate = ["picUrl"])
    val avatar: String? = "",//头像
    @SerializedName("position")
    var title: String? = "",//职位/班干部
    @SerializedName("userNo")
    val no: String? = "",//工号/学号
    val type: Int = 0,//1-普通任课老师 2-班主任 3-副班主任
    @SerializedName("lifeMotto", alternate = ["motto"])
    val slogan: String? = "",//格言
    val introduce: String? = "",//自我介绍
    @SerializedName("hobby")
    val fav: String? = "",//爱好
    @SerializedName("outstandingDeeds")
    val story: String? = "",//优秀事迹
    @SerializedName("comment")
    val evaluation: String? = "",//评价
    @SerializedName("specialty")
    val specialty: String? = "",//特长
    @SerializedName("subjectName")
    var tag: String? = "",//班干部职位/教授科目
    val gender: Int = 0,//性别，0-未填写；1-男；2-女
    val userType: Int? = 6,//0-学校创建者；1-学生；2-教师；3-家长；4-校友；5-退休老师；6-其他
) : Parcelable {
    val isEmpty
        get() = name.isEmpty() && no.isNullOrEmpty()

    companion object {
        val EMPTY = Person("", "")
    }
}

data class ClassDateGroup<T>(val time: Long, val data: List<T>) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ClassDateGroup<*>

        if (time != other.time) return false
        if (data != other.data) return false

        return true
    }

    override fun hashCode(): Int {
        var result = time.hashCode()
        result = 31 * result + data.hashCode()
        return result
    }
}

data class ClassInfo(
    val classNum: Int,//班额
    val fwzx: Int,//非晚自习通校生
    val motto: String,//班训
    val presentNum: Int,//当前班级人数
    val wzx: Int,//晚自习通校生
    val zxs: Int,//住校生
)

data class ClassIndexAttnRank(
    val attnRate: Int,//今日考勤率
    val gradeRank1: Int,//同年级比排名
    val gradeRank2: Int,//同年级排名上升或者下架位次
    val schoolRank1: Int,//学校比排名
    val schoolRank2: Int,//同学校排名上升或者下架位次
    val total: Int,//合计发布
    val unSign: Int,//未打卡人数
)

data class ClassAttendanceReport(
    val month: Int,
    val name: String,//姓名
    val rank: Int,
    val term: Int,//本学期
    val threeMonth: Int,
    val today: String,//今日考勤状态 0-未打卡 1-正常 2-迟到 3-早退 4-旷工
    val week: Int,
    val userNo: String,
)

data class ClassIndexRank(
    @SerializedName("gradeRank", alternate = ["classRank"])
    val gradeRank: String?,//同年级比排名
    val schoolRank: String?,//学校比排名
    val schoolUpOrDown: Int,//同学校排名上升或者下架位次
    @SerializedName("gradeUpOrDown", alternate = ["classUpOrDown"])
    val gradeUpOrDown: Int,//未打卡人数
    val peopleNum: Int,//合计发布
    var score: String//德育分
)

data class ClassIndexStudentStatus(
    val other: Int,//其他
    val person: Int,//事假
    val school: Int,//在校人数
    val sick: Int,//病假
)

data class ClassIndexHomeworkChart(
    val avgHour: List<LinkedHashMap<String, String>>?,//各科作业耗时
    val homeworkWeek: LinkedHashMap<String, LinkedHashMap<String, Int>>?,//同年级比排名
)

/**
 * description:
学期表

createTime	string($date-time)
delFlag	integer($int32)
0是未删除 1是删除

endDate	string($date-time)
结束日期

id	integer($int32)
modifyTime	string($date-time)
schoolId	string
startDate	string($date-time)
开学日期

termName	string
 */
data class Semester(
    val termName: String,
    val startDate: String,
    val endDate: String,
)

/**
 * 签到统计
 */
data class ClassSignStat(
    val subjectName: String,
    val signCount: Int,
    val signAllCount: Int,
)

data class ClassSignStatQuery(
    val departId: String = GlobalParameter.getEcardInfo().classId.toString(),
    val schoolId: String = GlobalParameter.getEcardInfo().schoolId.toString(),
)

/**
 * 登录返回的个人信息
 */
@Parcelize
data class PersonLoginResult(
    val name: String?,
    val userNo: String,
    val id: String,
    val classLeaderVOS: List<Person>?,
    val position: Int?,
    val userInfoVo: Person?,
) : Parcelable {
    /**
     * 判断身份
     *    普通学生 userInfoVo.userType == 1 && classLeaderVOS.isEmpty()
     *    班干部   userInfoVo.userType == 1 && classLeaderVOS.isNotEmpty()
     *    老师    userInfoVo.userType == 2
     */
    val personType: PersonType
        get() = if (userInfoVo?.userType == 1) {
            if (classLeaderVOS.isNullOrEmpty()) {
                PersonType.STUDENT//普通学生
            } else {
                PersonType.CLASS_LEADER//班干部
            }
        } else if (userInfoVo?.userType == 2) {
            PersonType.TEACHER//老师
        } else {
            PersonType.UNKNOWN//未知
        }
}

enum class PersonType {
    STUDENT,
    CLASS_LEADER,//班干部
    TEACHER,
    UNKNOWN,
}